<!--
 * @author: sanbao
 * @Date: 2022-07-22 14:56:14
-->
<style lang='scss' scoped>
.login {
  .bgimg {
    min-width: 100%;
    min-height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
  }
  overflow: hidden;
  .login_form {
    background-color: rgba(166, 165, 165, 0.241);
    padding: 25px;
    margin: 300px auto;
    width: 300px;
    border-radius: 10px;
    h2 {
      text-align: center;
      padding-bottom: 20px;
    }
    .el-button {
      width: 100%;
    }
    .form_item {
      display: flex;
      position: relative;
      .el-icon {
        font-size: 30px;
        padding: 5px;
        box-sizing: border-box;
      }
      .el-form-item {
        flex: 1;
      }
      :deep(.el-input__suffix-inner) {
        display: none;
      }
      .iconimg {
        position: absolute;
        right: 5px;
        top: 2px;
      }
    }
  }
}
</style>

<template>
  <div class="login">
    <img class="bgimg" src="../../../assets/image/bbbb.jpg" alt="" />
    <div class="login_form">
      <h2>小小后台可笑可笑</h2>
      <el-form
        ref="formRef"
        :model="ruleForm"
        :rules="rules"
        label-width="0"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <div class="form_item">
          <el-icon><Avatar /></el-icon>
          <el-form-item class="form_item" prop="username">
            <el-input v-model="ruleForm.username" />
          </el-form-item>
        </div>

        <div class="form_item">
          <el-icon><Lock /></el-icon>
          <el-form-item class="form_item" prop="password">
            <el-input
              v-model="ruleForm.password"
              :type="iconflag == false ? '' : 'password'"
            />
          </el-form-item>
          <div class="iconimg">
            <el-icon v-if="iconflag" @click="iconflag = false"
              ><Sunrise
            /></el-icon>
            <el-icon v-else @click="iconflag = true"><Sunny /></el-icon>
          </div>
        </div>

        <el-button type="primary" :size="formSize" @click="gohome(formRef)">
          登陆
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script lang='ts'>
import { defineComponent, ref, reactive, toRefs, computed } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { FormData } from "@/type/login";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
export default defineComponent({
  components: {},
  setup(props, { emit }) {
    const store = useStore();
    const router = useRouter();
    const data = reactive(new FormData());
    const formSize = ref("default");
    const formRef = ref<FormInstance>();
    const iconflag = ref(true);
    const rules = {
      username: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
        { min: 3, max: 5, message: "用户名在3-5之间", trigger: "blur" },
      ],
      password: [
        {
          required: true,
          message: "请输入用户密码",
          trigger: "blur",
        },
        { min: 3, max: 16, message: "密码长度在3-16之间", trigger: "blur" },
      ],
    };
    const gohome = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.validate(async (valid) => {
        if (!valid) return;
        const response = await store.dispatch("handellogin", data.ruleForm);
        if (response.data.meta.status == 200) {
          router.push("/");
        } else {
          ElMessage.error(response.data.meta.msg);
        }
      });
    };
    return {
      ...toRefs(data),
      iconflag,
      rules,
      formSize,
      gohome,
      formRef,
    };
  },
});
</script>
